<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The Volatile class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pool.submitTo.html">« Pool::submitTo</a></li>
      <li style="float: right;"><a href="book.sem.html">Semaphore »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.pthreads.html">pthreads</a></li>
    <li>The Volatile class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.volatile" class="reference">

 <h1 class="title">The Volatile class</h1>
 
  
 <div class="partintro"><p class="verinfo">(PECL pthreads &gt;= 3.0.0)</p>


  <div class="section" id="volatile.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    The <span class="classname"><strong class="classname">Volatile</strong></span> class is new to pthreads v3. Its
    introduction is a consequence of the new immutability semantics of
    <span class="classname"><a href="class.threaded.html" class="classname">Threaded</a></span> members of <span class="classname"><a href="class.threaded.html" class="classname">Threaded</a></span>
    classes. The <span class="classname"><strong class="classname">Volatile</strong></span> class enables for mutability
    of its <span class="classname"><a href="class.threaded.html" class="classname">Threaded</a></span> members, and is also used to store
    PHP arrays in <span class="classname"><a href="class.threaded.html" class="classname">Threaded</a></span> contexts.
   </p>
  </div>


  <div class="section" id="volatile.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Volatile</strong>
     </span>
     
     <span class="ooclass">
      <span class="modifier">extends</span>
       <a href="class.threaded.html" class="classname">Threaded</a>
     </span>
     
     <span class="oointerface"><span class="modifier">implements</span> 
       <a href="class.collectable.html" class="interfacename">Collectable</a></span><span class="oointerface">,  <a href="class.traversable.html" class="interfacename">Traversable</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 继承的方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="threaded.chunk.html" class="methodname">Threaded::chunk</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve</code></span>): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.count.html" class="methodname">Threaded::count</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.extend.html" class="methodname">Threaded::extend</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$class</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="thread.isrunning.html" class="methodname">Threaded::isRunning</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.isterminated.html" class="methodname">Threaded::isTerminated</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span><span class="methodname"><a href="threaded.merge.html" class="methodname">Threaded::merge</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$from</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$overwrite</code><span class="initializer"> = ?</span></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.notify.html" class="methodname">Threaded::notify</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.notifyone.html" class="methodname">Threaded::notifyOne</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.pop.html" class="methodname">Threaded::pop</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.run.html" class="methodname">Threaded::run</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.shift.html" class="methodname">Threaded::shift</a></span>(): <span class="type">boolean</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.synchronized.html" class="methodname">Threaded::synchronized</a></span>(<span class="methodparam"><span class="type"><a href="class.closure.html" class="type Closure">Closure</a></span> <code class="parameter">$block</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="threaded.wait.html" class="methodname">Threaded::wait</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$timeout</code><span class="initializer"> = ?</span></span>): <span class="type">bool</span></div>

   }</div>


  </div>

  <div class="section" id="volatile.examples">
   <h2 class="title">范例</h2>
   <div class="example" id="volatile.example.immutable">
    <p><strong>示例 #1 New immutability semantics of Threaded</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Task&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Threaded<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Threaded</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;attempt&nbsp;to&nbsp;overwrite&nbsp;a&nbsp;Threaded&nbsp;property&nbsp;of&nbsp;a&nbsp;Threaded&nbsp;class&nbsp;(invalid)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((new&nbsp;</span><span style="color: #0000BB">Task</span><span style="color: #007700">())-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite data in %s:%d
</pre></div>
    </div>
   </div>
   <div class="example" id="volatile.example.basic">
    <p><strong>示例 #2 Volatile use-case</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Task&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Volatile<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Threaded</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;attempt&nbsp;to&nbsp;overwrite&nbsp;a&nbsp;Threaded&nbsp;property&nbsp;of&nbsp;a&nbsp;Volatile&nbsp;class&nbsp;(valid)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((new&nbsp;</span><span style="color: #0000BB">Task</span><span style="color: #007700">())-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#3 (0) {
}
</pre></div>
    </div>
   </div>
  </div>

 </div>

</div>
</div></div></body></html>